let storage = bui.storage();
let language,translate;
loader.define(function (require, exports, module, global) {
    let pageview = {
        init() {
            translate = global.translate
            language = storage.get("language", 0)
            if (!language) {
                language = 'chinese_simplified'
            }

            try {
                translate.selectLanguageTag.show = false;
                translate.ignore.id.push('languageContent')
                let curLanguage = translate.language.getCurrent()
                if (curLanguage !== language) {
                    translate.changeLanguage(language)
                }
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
            if (language === "chinese_simplified") {
                router.$('#cateTab').css("font-size", ".34rem");
                router.$('#tip-hd').css("font-size", ".26rem");
                router.$('#weixin').css("font-size", ".32rem");
                router.$('#dh').css("font-size", ".32rem");
                router.$('#youxiang').css("font-size", ".32rem");
                router.$('#titleAudit').css("font-size", ".4rem");
            } else {
                router.$('#cateTab').css("font-size", ".26rem");
                router.$('#tip-hd').css("font-size", ".18rem");
                router.$('#weixin').css("font-size", ".26rem");
                router.$('#dh').css("font-size", ".26rem");
                router.$('#youxiang').css("font-size", ".26rem");
                router.$('#titleAudit').css("font-size", ".32rem");
            }
            let languageArr = storage.get('languageArr',0)
            let html = ''
            let html2 = '<select class="languageSelect" id="languageSelectId">\n'
            if (language !== 'chinese_simplified' && language !== 'english') {
                languageArr.forEach(lan => {
                    if (lan.id === language) {
                        html += '<span class="language" id="'+lan.id+'">'+lan.name+'</span>|'
                    } else {
                        html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                    }
                })
            } else {
                languageArr.forEach(lan => {
                    html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                })
            }
            html+= '<span class="language" id="english">English</span>|<span class="language" id="chinese_simplified">简体中文</span>'
            router.$("#languageContent").html(html + html2 + "</select>")

            router.$(".language").click(function () {
                let selectId = $(this).attr('id')
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })
            router.$("#languageSelectId").val("");
            router.$("#languageSelectId").change(function () {
                let selectId = $(this).val()
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })

            let uiLoading = bui.loading({
                appendTo:"#loadingAudit",
                width: 40,
                height: 40,
                opacity: 0.5,
                text:"提交中。。。",
                autoClose: false,
                callback: function (argument) {
                }
            });
            let uiLoading2 = bui.loading({
                appendTo:"#loadingAudit",
                width: 40,
                height: 40,
                opacity: 0.5,
                text:"加载中。。。",
                autoClose: false,
                callback: function (argument) {
                }
            });

            const url = window.location.href
            let id,glUserid = '',cateStr  = '', userId = '', title = '',fhSign, city='', country='', cate = '', cityId = '', jobId = '';
            let params = module.props;

            if (url.indexOf("?") > -1) {
                let param = url.split("?")[1]
                id = param.split("=")[1]
                router.$("#articleAudit").click(function () {
                    storage.set("tzPage", '0')
                    router.replace({ url: "pages/mainIndex/mainIndex.html"});
                })
                fhSign = '0'
            } else {
                id = params.id
                router.$("#articleAudit").click(function () {
                    router.load({ url: "pages/articleList/articleList.html"});
                })
                fhSign = '1'
            }
            if (!id) {
                id = storage.get("auditArticleId", 0)
                window.location.href = window.location.href + "?id=" + id
                router.$("#articleAudit").click(function () {
                    storage.set("tzPage", '0')
                    router.replace({ url: "pages/mainIndex/mainIndex.html"});
                })
            }

            let token = storage.get("token",0)
            if (token) {
                uiLoading2.show()
                bui.ajax({
                    url: global.api.verifyToken,
                    method: "POST",
                    headers: {Token: token}
                }).then(function(res) {
                    if (res.code != '200') {
                        uiLoading2.stop()
                        bui.confirm({
                            "title": "",
                            "height": 460,
                            "content": '<div class="bui-box-center"><div><h3>登录已过期</h3></div></div>',
                            "buttons": [{ name: "确定", className: "primary-reverse" }],
                            callback:function(){
                                this.close();
                                storage.set("dlTz", '15')
                                storage.set("auditArticleId", id)
                                router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                            }
                        });
                        try {
                            translate.execute();
                        }catch (e) {
                            console.log("切换语言报错")
                        }
                    } else {
                        //加载广告详情
                        bui.ajax({
                            url: global.api.getAdvertiseInfo,
                            data: {id: id},
                            method: "POST"
                        }).then(function(res){
                            let data = res.data

                            glUserid = data.glUserid
                            cateStr = data.cateStr
                            userId = data.user
                            city = data.city
                            country = data.webCountry
                            cityId = data.cityId
                            cate = data.cate
                            if (data.jobId) {
                                jobId = data.jobId
                            }
                            router.$("#cateTab").html(cateStr+"信息")
                            router.$("#time").html(formatDate(data.ctime) + " 发布")
                            router.$("#country2").html(data.countryStr + " "+ data.cityStr)
                            if (data.tel) {
                                router.$("#phone").html(data.tel)
                                router.$("#dh").show()
                            }
                            if (data.wx) {
                                router.$("#wx").html(data.wx)
                                router.$("#weixin").show()
                            }
                            if (data.email) {
                                router.$("#email").html(data.email)
                                router.$("#youxiang").show()
                            }

                            if (!data.title1)  {
                                title = data.title
                                router.$("#titleAudit").html(data.title)
                                router.$("#titleAudit").show()
                            } else {
                                title = data.title1
                            }
                            router.$("#contentAudit").html(data.content)
                            if (data.imgs) {
                                let imgHtml = ''
                                router.$("#imgsAudit").show()
                                $.each(data.imgs, function (index, value) {
                                    console.log(value)
                                    imgHtml += '<img src="'+value+'" style="width: 300px; 150px">'
                                })
                                router.$("#imgsAudit").html(imgHtml)
                            }

                            if (data.delFlag == '0') {
                                if (data.status != '2') {
                                    let titleStr = ''
                                    if(data.status == '0'){
                                        titleStr = "该文章已审核失败，确定要重更审核么?"
                                    } else {
                                        titleStr = "该文章已审核成功，确定要重更审核么?"
                                    }
                                    bui.confirm(titleStr, function(e) {
                                        //this 是指点击的按钮
                                        let text = $(e.target).text();

                                        if (text == "取消") {
                                            storage.set("tzPage", 0)
                                            router.replace({ url: "pages/mainIndex/mainIndex.html"});
                                        }

                                        this.close()
                                    });
                                }
                            } else {
                                bui.confirm({
                                    "title": "",
                                    "height": 460,
                                    "content": '<div class="bui-box-center"><div><p>文章已过期不可审核！</p></div></div>',
                                    "buttons": ["确定"],
                                    callback: function(e) {
                                        //this 是指点击的按钮
                                        let text = router.$(e.target).text();

                                        if (text == "确定") {
                                            if (fhSign == '0') {
                                                storage.set("tzPage", 0)
                                                router.replace({url: "pages/mainIndex/mainIndex.html"});
                                            } else {
                                                bui.back()
                                            }
                                        }
                                        this.close()
                                    }
                                });
                            }
                            uiLoading2.stop()

                            try {
                                translate.execute();
                            }catch (e) {
                                console.log("切换语言报错")
                            }
                        })
                    }
                })

                router.$("#refause").click(function () {
                    // bui.prompt("请输入拒绝原因",function(e){
                    //     var text = $(e.target).text();
                    //     if( text == "取消"){
                    //         this.close();
                    //     }else{
                    //         let thi = this
                    //         bui.ajax({
                    //             url: global.api.auditArticle,
                    //             data: {id: id, status: '0', error: this.value()},
                    //             method: "POST"
                    //         }).then(function(res){
                    //             if (res.code == '200') {
                    //                 thi.close();
                    //                 storage.set("tzPage", 0)
                    //                 bui.load({ cache:false, url: "pages/mainIndex/mainIndex.html", replace: true});
                    //             } else {
                    //                 bui.alert(res.msg)
                    //             }
                    //         })
                    //     }
                    // })

                    bui.confirm("确定提交审核拒绝么?", function(e) {
                        //this 是指点击的按钮
                        let text = $(e.target).text();

                        if (text == "确定") {
                            uiLoading.show()
                            let thi = this
                            bui.ajax({
                                url: global.api.auditArticle,
                                data: {id: id, status: '0', glUserid: glUserid, cateStr: cateStr, user: userId, title: title, city: city, country: country, cate: cate, cityId:cityId},
                                method: "POST",
                                headers: {Token: token}
                            }).then(function(res){
                                if (res.code == '200') {
                                    thi.close();
                                    storage.set("tzPage", 0)
                                    router.replace({ url: "pages/mainIndex/mainIndex.html"});
                                } else {
                                    bui.alert("操作失败")
                                }
                                uiLoading.stop()
                            })
                        } else {
                            this.close()
                        }
                    });
                })
                router.$("#update").click(function () {
                    bui.confirm("确定修改当前文章么?", function(e) {
                        //this 是指点击的按钮
                        let text = $(e.target).text();

                        if (text == "确定") {
                            storage.set('articleEditId', id)
                            router.load({ url: "pages/articleEdit/articleEdit.html", param: {sign: '0', tzSign: '1'}});
                        } else {
                            this.close()
                        }
                    });
                })
                router.$("#success").click(function () {
                    bui.confirm("确定提交审核成功么?", function(e) {
                        //this 是指点击的按钮
                        let text = $(e.target).text();

                        if (text == "确定") {
                            uiLoading.show()
                            let thi = this
                            bui.ajax({
                                url: global.api.auditArticle,
                                data: {id: id, status: '1', glUserid: glUserid, cateStr: cateStr, user: userId, title: title,city: city, country: country, cate: cate, cityId:cityId, jobId:jobId},
                                method: "POST",
                                headers: {Token: token}
                            }).then(function(res){
                                if (res.code == '200') {
                                    thi.close();
                                    storage.set("tzPage", 0)
                                    router.replace({ url: "pages/mainIndex/mainIndex.html"});
                                } else {
                                    bui.alert("操作失败")
                                }
                                uiLoading.stop()
                            })
                        } else {
                            this.close()
                        }
                    });
                })
            } else {
                bui.confirm({
                    "title": "",
                    "height": 460,
                    "content": '<div class="bui-box-center"><div><h3>当前未登录，请登录</h3></div></div>',
                    "buttons": [{ name: "登录", className: "primary-reverse" }],
                    callback:function(){
                        this.close();
                        storage.set("dlTz", '15')
                        storage.set("auditArticleId", id)
                        router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                    }
                });
            }
            try {
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
        }
    };

    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})


function formatDate(time) {
    let date = new Date(time * 1000);
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    let D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
    let h = (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes()) + ':';
    let s = (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());
    return Y+M+D+h+m+s
}
